package com.mecol.education.service;

import com.mecol.education.dao.AdminMenuDAO;
import com.mecol.education.dao.StudentDao;
import com.mecol.education.dao.TeacherDao;
import com.mecol.education.entity.Admin;
import com.mecol.education.entity.AdminMenu;
import com.mecol.education.entity.AdminRoleMenu;
import com.mecol.education.entity.AdminUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdminMenuService {

    @Autowired
    private AdminService adminService;
    @Autowired
    private AdminMenuDAO adminMenuDAO;
    @Autowired
    private AdminUserRoleService adminUserRoleService;
    @Autowired
    private AdminRoleMenuService adminRoleMenuService;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private StudentDao studentDao;


    public List<AdminMenu> getAllByParentId(int parentId) {
        return adminMenuDAO.findAllByParentId(parentId);
    }
    //查询出来的菜单数据列表整合成具有层级关系的菜单树
    public void handleMenus(List<AdminMenu> menus) {
        menus.forEach(m -> {
            List<AdminMenu> children = getAllByParentId(m.getId());
            m.setChildren(children);
        });

        menus.removeIf(m -> m.getParentId() != 0);
    }



    public List<AdminMenu> getMenusByCurrentUser(String username,String radio) {

        List<Integer> rids=new ArrayList<Integer>();


        if(radio.equals("1")){ //管理员登录 一个管理员可能有好几种角色
            Admin admin=adminService.findByUsername(username);
            //一个用户可能有很多个角色 下面是获取这些角色的集合 就是List<Integer> rids
             rids=adminUserRoleService.listAllByUid(admin.getId())
                    .stream().map(AdminUserRole::getRid).collect(Collectors.toList());
        }else if(radio.equals("2")){  //教师登录 只有教师这一种角色
            rids= Collections.singletonList((teacherDao.findByUsername(username)).getRole_id());
        }
        else if(radio.equals("3")) { //学生登录
            rids= Collections.singletonList((studentDao.findByUsername(username)).getRole_id());

        }





        //获取这些角色对应的菜单
        List<Integer> menuIds=adminRoleMenuService.findAllByRid(rids)
                .stream().map(AdminRoleMenu::getMid).collect(Collectors.toList());

        //通过 distinct() 对查询出的菜单项进行了去重操作，避免多角色情况下有冗余的菜单等
        List<AdminMenu> menus=adminMenuDAO.findAllById(menuIds)
                .stream().distinct().collect(Collectors.toList());

       // System.out.println(menus);

        handleMenus(menus);
      //  System.out.println(menus);
        return menus;

    }


    public List<AdminMenu> getMenusByRoleId(int rid){
        List<Integer> menusIds=adminRoleMenuService.findAllByRid(rid)
                .stream().map(AdminRoleMenu::getMid).collect(Collectors.toList());
        List<AdminMenu> menus=adminMenuDAO.findAllById(menusIds);
        handleMenus(menus);
        return menus;
    }
}
